//package com.hu.manage.shiro;
//
//import com.hu.uitl.JwtUtils;
//import io.jsonwebtoken.SignatureException;
//import org.apache.shiro.authc.AuthenticationException;
//import org.apache.shiro.authc.AuthenticationInfo;
//import org.apache.shiro.authc.AuthenticationToken;
//import org.apache.shiro.authc.SimpleAuthenticationInfo;
//import org.apache.shiro.authz.AuthorizationInfo;
//import org.apache.shiro.authz.SimpleAuthorizationInfo;
//import org.apache.shiro.realm.AuthorizingRealm;
//import org.apache.shiro.subject.PrincipalCollection;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import java.util.Map;
//
//
//@Component
//public class StatelessRealm extends AuthorizingRealm {
//
//    private static final Logger logger = LoggerFactory.getLogger(StatelessRealm.class.getSimpleName());
//    private JwtUtils jwtUtils;
//
//    @Autowired
//    public void setJwtUtils(JwtUtils jwtUtils) {
//        this.jwtUtils = jwtUtils;
//    }
//
//    @Override
//    public boolean supports(AuthenticationToken token) {
//        return token instanceof StatelessToken;
//    }
//
//    /**
//     * 授权，解析token,并获取token中的role值,然后添加权限
//     */
//    @Override
//    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//
//        String jwt = (String) principals.getPrimaryPrincipal();
//        Map<String, Object> claims = jwtUtils.validateToken(jwt);
//        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
//
//      //  String role = String.valueOf(claims.get("role"));可以从token中获取
//
//        authorizationInfo.addRole("admin");
//        return authorizationInfo;
//    }
//
//    /**
//     * 认证，比如请求头中是否带本服务器生成的token
//     */
//    @Override
//    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//        String jwt = (String) token.getPrincipal();
//        if (jwt==null||jwt.trim().isEmpty()) {
//            throw new AuthenticationException("token 验证失败");
//        }
//        try {
//            jwtUtils.validateToken(jwt);
//        } catch (SignatureException e) {
//            throw new AuthenticationException("token 验证失败");
//        }
//        // TODO 接入 redis 验证该用户是否登录
//        return new SimpleAuthenticationInfo(jwt, jwt, getName());
//    }
//
////    @Override
////    public String getName() {
////        return "statelessRealm";
////    }
//}
